package com.bstek.uflo.console.controller;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.bstek.uflo.process.handler.ActionHandler;
import com.bstek.uflo.process.handler.AssignmentHandler;
import com.bstek.uflo.process.handler.ConditionHandler;
import com.bstek.uflo.process.handler.CountersignHandler;
import com.bstek.uflo.process.handler.DecisionHandler;
import com.bstek.uflo.process.handler.ForeachHandler;
import com.bstek.uflo.process.handler.NodeEventHandler;
import com.bstek.uflo.process.handler.ProcessEventHandler;
import com.bstek.uflo.process.handler.ReminderHandler;
import com.bstek.uflo.process.node.FormTemplateProvider;

/**
 * @author Jacky.gao
 * @since 2013年9月13日
 */
public class HandlerListController extends AbstractController implements InitializingBean{
	private Map<String,Set<String>> map=new HashMap<String,Set<String>>();
	@Override
	protected ModelAndView doHandleRequest(HttpServletRequest req,HttpServletResponse response) throws Exception {
		preCheck();
		String handler=req.getParameter("handler");
		if(StringUtils.isEmpty(handler)){
			throw new IllegalArgumentException("Parameter handler can not be null");
		}
		ObjectMapper mapper=new ObjectMapper();
		response.setContentType("text/json");  
		response.setCharacterEncoding("UTF-8");  
		OutputStream out=response.getOutputStream();
		try{
			mapper.writeValue(out, map.get(handler));
		}finally{
			out.flush();
			out.close();
		}
		return null;
	}

	public void afterPropertiesSet() throws Exception {
		ApplicationContext context=getApplicationContext();
		map.putAll(buildHandlerMap(context));
		context=context.getParent();
		if(context!=null){
			map.putAll(buildHandlerMap(context));			
		}
		
	}

	private Map<String,Set<String>> buildHandlerMap(ApplicationContext context) {
		Map<String,Set<String>> handerMap=new HashMap<String,Set<String>>();
		handerMap.put(NodeEventHandler.class.getSimpleName(), context.getBeansOfType(NodeEventHandler.class).keySet());
		handerMap.put(ProcessEventHandler.class.getSimpleName(), context.getBeansOfType(ProcessEventHandler.class).keySet());
		handerMap.put(DecisionHandler.class.getSimpleName(), context.getBeansOfType(DecisionHandler.class).keySet());
		handerMap.put(AssignmentHandler.class.getSimpleName(), context.getBeansOfType(AssignmentHandler.class).keySet());
		handerMap.put(ConditionHandler.class.getSimpleName(), context.getBeansOfType(ConditionHandler.class).keySet());
		handerMap.put(ActionHandler.class.getSimpleName(), context.getBeansOfType(ActionHandler.class).keySet());
		handerMap.put(ForeachHandler.class.getSimpleName(), context.getBeansOfType(ForeachHandler.class).keySet());
		handerMap.put(ReminderHandler.class.getSimpleName(), context.getBeansOfType(ReminderHandler.class).keySet());
		handerMap.put(CountersignHandler.class.getSimpleName(), context.getBeansOfType(CountersignHandler.class).keySet());
		Set<String> set=new HashSet<String>();
		for(FormTemplateProvider provider:context.getBeansOfType(FormTemplateProvider.class).values()){
			set.add(provider.getFormTemplate());
		}
		handerMap.put(FormTemplateProvider.class.getSimpleName(), set);
		return handerMap;
	}
}
